package entity

import (
	"encoding/json"
	"time"
)

// 事件功能
type RoleEvent struct {
	RoleID      int32  `gorm:"type:int;primary_key"`
	EventIDs    []byte `gorm:"type:jsonb" json:"-"` // 天赋 json
	RefreshTime int64
	CreatedAt   time.Time
	UpdatedAt   time.Time

	EventIDArr []int32 `sql:"-"`
}

func (e *RoleEvent) AfterFind() error {

	var arr = make([]int32, 0)
	var err = json.Unmarshal(e.EventIDs, &arr)
	e.EventIDArr = arr
	return err
}

func (e *RoleEvent) BeforeCreate() (err error) {
	e.EventIDs, err = json.Marshal(e.EventIDArr)
	return err
}

func (e *RoleEvent) BeforeSave() (err error) {
	e.EventIDs, err = json.Marshal(e.EventIDArr)
	return err
}

func (e *RoleEvent) BeforeUpdate() (err error) {
	e.EventIDs, err = json.Marshal(e.EventIDArr)
	return err
}
